﻿#include <iostream>
using namespace std;

// 协变
class Person 
{
public:
	virtual Person* BuyTicket()
	{
		cout << "买票-全价" << endl;
		return nullptr;
	}
};

class Student : public Person 
{
public:
	virtual  Student* BuyTicket()
	{ 
		cout << "买票-打折" << endl;
		return nullptr;
	}
};

void Func(Person& people)
{
	people.BuyTicket();
} 

int main()
{
	Person Mike;
	Func(Mike);

	Student Johnson;
	Func(Johnson);
	return 0;
}


//class A
//{
//public :
//	virtual void func(int val = 1) { cout << "A->" << val << endl; }
//	virtual void test() { func(); }
//};
//
//class B : public A
//{
//public :
//	void func(int val = 0) { cout << "B->" << val << endl; }
//};
//
//int main(int argc, char* argv[])
//{
//	B* p = new B;
//	p->test();
//	return 0;
//}


//// 析构函数的重写
//class A
//{
//public :
//	virtual ~A()
//	{
//		cout << "~A()" << endl;
//	}
//};
//
//class B : public A 
//{
//public:
//	~B()
//	{
//		cout << "~B()->delete:" << _p << endl;
//		delete _p;
//	}
//protected:
//	int* _p = new int[10];
//};
//// 只有派⽣类Student的析构函数重写了Person的析构函数，下⾯的delete调⽤析构函数才能构成多态，
//// 才能保证p1和p2指向的对象正确的调用析构函数。
//
//int main()
//{
//	A* p1 = new A;
//	A* p2 = new B;
//	delete p1;
//	delete p2;
//	return 0;
//}


//// 纯虚函数和抽象类
//class Car
//{
//public :
//	virtual void Drive() = 0;
//};
//
//class Benz :public Car
//{
//public:
//	virtual void Drive() { cout << "Benz-舒适" << endl; }
//};
//
//class Bmw :public Car
//{
//public:
//	virtual void Drive() { cout << "Bmw-操控" << endl; }
//};
//
//int main()
//{
//	Car* pBenz = new Benz;
//	pBenz->Drive();
//
//	Car* pBmw = new Bmw;
//	pBmw->Drive();
//	return 0;
//}




